package org.example;

import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

import java.util.concurrent.CountDownLatch;

public class Recv {
    private final static String QUEUE_NAME = "hello.queue2";
    static CountDownLatch latch = new CountDownLatch(1);
    public static void main(String[] argv) throws Exception {


        // 创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("10.211.55.10"); // 设置 RabbitMQ 服务器地址
        factory.setUsername("fengliulin"); // 设置 RabbitMQ 服务器地址
        factory.setPassword("123456"); // 设置 RabbitMQ 服务器地址

        // 创建连接
        try (Connection connection = factory.newConnection();
             Channel channel = connection.createChannel()) {

            // 声明队列
            channel.queueDeclare(QUEUE_NAME, true, false, false, null);
            System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

            // 定义消息接收的回调
            DeliverCallback deliverCallback = (consumerTag, delivery) -> {
                String message = new String(delivery.getBody(), "UTF-8");
                System.out.println(" [x] Received '" + message + "'");
            };

            // 开始消费消息
            channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> { });

            try {
                latch.await();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }

//        Thread workerThread = new Thread(() -> {
//            try {
//                // 模拟一些工作
//                Thread.sleep(2000);
//            } catch (InterruptedException e) {
//                Thread.currentThread().interrupt();
//            } finally {
//            }
//        });
//
//        workerThread.start();


    }
}
